package com.songlanyun.modules.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.validator.ValidatorUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.servicegoods.dao.ServiceGoodsGroupRelationDao;
import com.songlanyun.modules.servicegoods.entity.ServiceGoodsGroupRelation;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsGroupRelationService;


@Service("serviceGoodsGroupRelationService")
public class ServiceGoodsGroupRelationServiceImpl extends ServiceImpl<ServiceGoodsGroupRelationDao, ServiceGoodsGroupRelation> implements ServiceGoodsGroupRelationService {

    @Override
    public ServiceGoodsGroupRelation create(ServiceGoodsGroupRelation entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public ServiceGoodsGroupRelation modify(ServiceGoodsGroupRelation entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {
        this.removeById(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ServiceGoodsGroupRelation> page = this.page(
                new Query<ServiceGoodsGroupRelation>().getPage(params),
                new QueryWrapper<ServiceGoodsGroupRelation>()
        );

        return new PageUtils(page);
    }

    private void  clearRelation(Long goodsId)
    {
        this.baseMapper.delete(new LambdaQueryWrapper<ServiceGoodsGroupRelation>()
        .eq(ServiceGoodsGroupRelation::getServiceGoodsId,goodsId));
    }

    @Override
    public void breachSave(Long serviceGoodsId,List<ServiceGoodsGroupRelation> relations) {
        clearRelation(serviceGoodsId);
        if(relations.size()>0)
        {
            this.saveBatch(relations);
        }

    }
}